package com.it2297.view;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import java.awt.Dimension;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;

import com.it2297.renderer.BiddingHistoryCellRenderer;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

@SuppressWarnings("serial")
public class BiddingHistoryView extends JPanel
{
	private final JScrollPane scrollPane = new JScrollPane();
	private final JTree tree = new JTree();
	private final JTable table = new JTable();
	private final DefaultTableModel tableModel = new DefaultTableModel(0, 3)
	{
		@Override
		public boolean isCellEditable(int row, int column)
		{
			return false;
		}
	};
	private final BiddingHistoryCellRenderer tableCellRenderer = new BiddingHistoryCellRenderer();

	public BiddingHistoryView()
	{

		initGUI();
	}

	private void initGUI()
	{
		setBounds(100, 100, 750, 570);
		setLayout(new BorderLayout(0, 0));

		add(scrollPane, BorderLayout.CENTER);

		table.setModel(tableModel);
		table.setRowHeight(100);

		table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(1).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(2).setCellRenderer(tableCellRenderer);

		table.getColumnModel().getColumn(0).setHeaderValue("Item Details");
		table.getColumnModel().getColumn(1).setHeaderValue("Date Bidded");
		table.getColumnModel().getColumn(2).setHeaderValue("Status");

		table.getColumnModel().getColumn(1).setMinWidth(125);
		table.getColumnModel().getColumn(1).setMaxWidth(125);
		table.getColumnModel().getColumn(2).setMinWidth(125);
		table.getColumnModel().getColumn(2).setMaxWidth(125);

		scrollPane.setViewportView(table);

		tree.setBorder(new EmptyBorder(5, 0, 0, 0));
		tree.setOpaque(false);
		tree.setModel(new DefaultTreeModel(
			new DefaultMutableTreeNode("Bidding History") {
				{
					DefaultMutableTreeNode node_1;
					node_1 = new DefaultMutableTreeNode("My Bids");
						node_1.add(new DefaultMutableTreeNode("January"));
						node_1.add(new DefaultMutableTreeNode("February"));
						node_1.add(new DefaultMutableTreeNode("March"));
						node_1.add(new DefaultMutableTreeNode("April"));
						node_1.add(new DefaultMutableTreeNode("May"));
						node_1.add(new DefaultMutableTreeNode("June"));
						node_1.add(new DefaultMutableTreeNode("July"));
						node_1.add(new DefaultMutableTreeNode("August"));
						node_1.add(new DefaultMutableTreeNode("September"));
						node_1.add(new DefaultMutableTreeNode("October"));
						node_1.add(new DefaultMutableTreeNode("November"));
						node_1.add(new DefaultMutableTreeNode("December"));
					add(node_1);
				}
			}
		));
		tree.setPreferredSize(new Dimension(150, 72));
		tree.setShowsRootHandles(true);

		add(tree, BorderLayout.WEST);
	}

	public void addTreeMouseActionListener(MouseListener mouseListener)
	{
		tree.addMouseListener(mouseListener);
	}

	public void addTableRow(Object tableRowData)
	{
		tableModel.addRow(new Object[] { tableRowData, tableRowData, tableRowData });
	}

	public void clearTableRow()
	{
		tableModel.setRowCount(0);
	}

	public String getSelectedTreeValue()
	{
		return tree.getSelectionPath().getLastPathComponent().toString();
	}

	public boolean isTreeSelectionEmpty()
	{
		return tree.isSelectionEmpty();
	}

}
